package com.ymatou.iapi.godmch.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.PercentageItem;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.parameter.getAgentMarkupConfigBean;
import com.ymatou.iapi.godmch.parameter.setAgentMarkupConfigBean;
import com.ymatou.iapi.godmch.service.getAgentMarkupConfigCall;
import com.ymatou.iapi.godmch.service.setAgentMarkupConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
/**
*获取代理价全局加价规则
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getAgentMarkupConfig{
	private static getAgentMarkupConfigBean bean;
	private static getAgentMarkupConfigCall call;
	public static agent_typeWapper agentTypeWapper = new agent_typeWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取代理价全局加价规则");
	}

	@Before
	public void caseUp() {
		bean=new getAgentMarkupConfigBean();
		call=new getAgentMarkupConfigCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void check_verify(setAgentMarkupConfigBean setAgentMarkupConfigBean) throws InterruptedException {

		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		Logger.verifyEquals(setAgentMarkupConfigBean.getMchId(), call.getResult().getInt("mchId"), "验证mchId");
		Logger.verifyEquals(setAgentMarkupConfigBean.getDecimalType(), call.getResult().getInt("decimalType"), "验证decimalType");
		Logger.verifyEquals(setAgentMarkupConfigBean.getRefreshProduct(), call.getResult().getBoolean("refreshProduct"), "验证refreshProduct");
		
		List<PercentageItem> return_Items=call.getPercentageList();
		for(int i=0;i<return_Items.size();i++)
		{
			PercentageItem percentageItem=setAgentMarkupConfigBean.getPercentageList().get(i);
			Logger.verifyEquals(return_Items.get(i).getPercentage(), percentageItem.getPercentage(), "验证Percentage");
			Logger.verifyEquals(return_Items.get(i).getAgentTypeId(), percentageItem.getAgentTypeId(), "验证AgentTypeId");
			
	        //查询代理类型
			Map<String, Object> map = new HashMap<>();
			map.put("mchId", setAgentMarkupConfigBean.getMchId());
			map.put("agentType", percentageItem.getAgentTypeId());
			agent_type DB_type = agentTypeWapper.selectby(map).get(0);
			Logger.verifyEquals(return_Items.get(i).getAgentTypeTitle(), DB_type.getTypeTitle(), "验证AgentTypeTitle");
		}
	}
		
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentMarkupConfig_001() {
		Logger.start(true,"获取代理价全局加价规则-商户id存在的-返回成功");
		try {
			setAgentMarkupConfigBean setAgentMarkupConfigBean=GodmchManageCallService.initSetAgentMarkupConfigBean();
			setAgentMarkupConfigCall setAgentMarkupConfigCall=GodmchManageCallService.initSetAgentMarkupConfigCall(setAgentMarkupConfigBean);
			
			bean.setMchId(setAgentMarkupConfigBean.getMchId());
			call.setData(bean);
			call.callService();		
			
			check_verify(setAgentMarkupConfigBean);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentMarkupConfig_001_1() {
		Logger.start(true,"获取代理价全局加价规则-商户有两个代理-返回成功");
		try {
			setAgentMarkupConfigBean setAgentMarkupConfigBean=GodmchManageCallService.initSetAgentMarkupConfigBean();
			
			createagentBean createagentBean=GodmchManageCallService.InitCreateAgentTypeBean(setAgentMarkupConfigBean.getMchId());
			GodmchManageCallService.CreateAgentTypeCall(createagentBean);
	        //查询代理类型
			Map<String, Object> map = new HashMap<>();
			map.put("mchId", setAgentMarkupConfigBean.getMchId());
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			List<PercentageItem> items=new ArrayList();
			PercentageItem item=new PercentageItem();
			item.setAgentTypeId(list.get(0).getAgentType());
			item.setPercentage(new BigDecimal(10));
			items.add(item);
			
			item=new PercentageItem();
			item.setAgentTypeId(list.get(1).getAgentType());
			item.setPercentage(new BigDecimal(20));
			items.add(item);
			setAgentMarkupConfigBean.setPercentageList(items);
			
			GodmchManageCallService.initSetAgentMarkupConfigCall(setAgentMarkupConfigBean);
			bean.setMchId(setAgentMarkupConfigBean.getMchId());
			call.setData(bean);
			call.callService();		
			
			check_verify(setAgentMarkupConfigBean);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentMarkupConfig_002() {
		Logger.start(true,"获取代理价全局加价规则-商户id不存在-返回失败");
		try {
			
			bean.setMchId(99999);
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentMarkupConfig_003() {
		Logger.start(true,"获取代理价全局加价规则-商户id为空-返回失败");
		try {
			
			
			//bean.setMchId(99999);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}